package com.gitee.huanminabc.db_freedom.wrapper;

import com.baomidou.mybatisplus.annotation.TableName;
import com.gitee.huanminabc.utils_common.string.StringUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
public abstract class AbstractSqlWrapper<T, Children extends AbstractSqlWrapper<T, Children>> extends AbstractBaseWrapper<T, Children>{

    //操作记录
    @Getter
    protected List<String> operationRecord=new ArrayList<>();
    //这种是为了支持多表查询
    private final Map<String,TableInfo> entityClassList=new HashMap<>();
    protected final Children typedThis = (Children) this; //对象本身

    private static    int aliasIndex=1;//别名索引

    private T entity;
    private  TableInfo tableInfo;

    protected int offset=0;
    protected  int limit=Integer.MAX_VALUE;

    public T getEntity() {
        return entity;
    }
    public AbstractSqlWrapper() {}

    public AbstractSqlWrapper(Class<?> entityClass, SQL sql) {
        super(sql);
        try {
           this.entity= (T)entityClass.newInstance();
            this.tableInfo= createTableInfo(entityClass);
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    protected TableInfo createTableInfo(Class<?> entityClass){
        TableInfo tableInfo = new TableInfo();
        String tableName = classToTableName(entityClass);
        tableInfo.setEntityClass(entityClass);
        tableInfo.setTableName(tableName);
        tableInfo.setAlias("t"+aliasIndex++);
        entityClassList.put(entityClass.getName(),tableInfo);
        return tableInfo;
    }



    //通过Class获取表名
    protected String classToTableName(Class<?> clazz){
        //识别注解上的@TableName
        TableName annotation = clazz.getAnnotation(TableName.class);
        //如果没有注解则使用类名
        if (annotation != null) {
            //获取表名
            return annotation.value();
        }else{
            String simpleName = clazz.getSimpleName();
            //首字母小写 Aa -> aa
            String tableName = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);
            //将驼峰转为下划线 aBc -> a_bc
            tableName = StringUtil.toUnderline(tableName);
            return tableName;
        }
    }





    @Override
    public String toString() {
        String sql = SQL().toString();
        //安装 SELECT FROM WHERE GROUP BY HAVING ORDER BY 分割美化
        sql = sql.replaceAll("SELECT ", "SELECT\n");
        sql = sql.replaceAll(" FROM ", "\nFROM ");
        sql = sql.replaceAll(" WHERE ", "\nWHERE ");
        sql = sql.replaceAll(" GROUP BY ", "\nGROUP BY ");
        sql = sql.replaceAll(" HAVING ", "\nHAVING ");
        sql = sql.replaceAll(" ORDER BY ", "\nORDER BY ");
        sql = sql.replaceAll(" LIMIT ", "\nLIMIT ");
        //每个逗号后面加换行
        sql = sql.replaceAll(",", ",\n");
        return sql;
    }
}
